$(function() {
	var page = 1;
	var jsonStr;
	var store_person_contact_str;
	var person_phone;
	var store_person_yzm_str;
	var store_person_wx;
	var store_person_gz;
	var controller = 1;
	var store_id = localStorage.getItem("store_id");
	var urlPath = "http://yx.fxtourong.com/V1/Goods/storeTopicList" + "?store_id=" + store_id;
	
	$.ajax({
			type: "POST",
			url: urlPath,
			data: {
				page: page
			},
			dataType: "json",
			success: function(data) {
				var code = data.code;
				console.log(data.message);
				if(code == "1") {
					jsonStr = data.data;
					for(var i = 0; i<jsonStr.length; i++){
						var industry = jsonStr[i];
						var point = industry.status;
						var sxStr = "";
						if(point == 1){
							sxStr = "下架";
						} else {
							sxStr = "上架";
						}
						$("#manager_goods").append(" <li class='am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left liid' value='" + industry.id + "'>" + " <div class='am-u-sm-4 am-list-thumb'>" + 
										"<img src='" + industry.cover + "' alt='话题图片' style='width: 100px;height: 90px;margin-left: 10px;' />" + "</div>" +
										" <div class='am-u-sm-8 am-list-main'>" + "<h3 class='am-list-item-hd'>"+ industry.name +"</h3>" +
										" <button type='button' class='am-btn am-btn-secondary am-round sxbtn' style='float: right;margin-right: 10px;display:none;' value='" + industry.status  +"'>" + sxStr + "</button>" +
										" <div style='margin-top: 8px;'>" + "<span style='font-size: 14px;color: #BABABA;'>" + timeToStr(industry.upper_time) + "</span>" + "</div>" + "</div>"
										+ "</li>");
						$(".sxbtn").click(function(){
							controller = 2;
							var status = $(".sxbtn").val();
							console.log("qqqqqqqqqq:" + status);
							if(status == 1){
								status = 2;
							} else {
								status = 1;
							}
					  		$.ajax({
								type: "POST",
								url: "http://yx.fxtourong.com/V1/Goods/topicUpAndDown",
								data: {
									token : localStorage.getItem("token"),
									goods_id: $(".liid").val(),
									status: status
								},
								dataType: "json",
								success: function(data) {
									console.log(data);
									var code = data.code;
									console.log(code);
									if(code == "1") {
										controller = 1;
										if(status == 1){
//											$(".sxbtn").val("下架");
											$.toaster('上架成功');
										} else {
//											$(".sxbtn").val("上架");
											$.toaster('下架成功');
										}
									} else {
										controller = 1;
										if(status == 1){
											$.toaster('上架失败');
										} else {
											$.toaster('下架失败');
										}
									}
								}
							});
						});
					}
				}
			}
		});
		
	function timeToStr(unixTime) {
                unixTime = parseInt(unixTime) + parseInt(8) * 60 * 60;
                var time = new Date(unixTime * 1000);
                var ymdhis = "";
                ymdhis += time.getUTCFullYear() + "-";
                ymdhis += (time.getUTCMonth()+1) + "-";
                ymdhis += time.getUTCDate();
                ymdhis += " " + time.getUTCHours() + ":";
                    ymdhis += time.getUTCMinutes() + ":";
                    ymdhis += time.getUTCSeconds();
                return ymdhis;
            }
        

	$("#more_btn").click(function(){
		page = page + 1;
		$.ajax({
			type: "POST",
			url: urlPath,
			data: {
				page: page
			},
			dataType: "json",
			success: function(data) {
				var code = data.code;
				console.log(code);
				if(code == "1") {
					jsonStr = data.data;
					for(var i = 0; i<jsonStr.length; i++){
						var industry = jsonStr[i];
						var point = industry.status;
						var sxStr = "";
						if(point == 1){
							sxStr = "下架";
						} else {
							sxStr = "上架";
						}
						$("#manager_goods").append(" <li class='am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left liid' value='" + industry.id + "'>" + " <div class='am-u-sm-4 am-list-thumb'>" + 
										"<img src='" + industry.cover + "' alt='话题图片' style='width: 100px;height: 90px;margin-left: 10px;' />" + "</div>" +
										" <div class='am-u-sm-8 am-list-main'>" + "<h3 class='am-list-item-hd'>"+ industry.name +"</h3>" +
										" <button type='button' class='am-btn am-btn-secondary am-round sxbtn' style='float: right;margin-right: 10px;display:none;' value='" + industry.status  +"'>" + sxStr + "</button>" +
										" <div style='margin-top: 8px;'>" + "<span style='font-size: 14px;color: #BABABA;'>" + timeToStr(industry.upper_time) + "</span>" + "</div>" + "</div>"
										+ "</li>");
						$(".sxbtn").click(function(){
							var status = $(".sxbtn").val();
							console.log("qqqqqqqqqq:" + status);
							if(status == 1){
								status = 2;
							} else {
								status = 1;
							}
					  		$.ajax({
								type: "POST",
								url: "http://yx.fxtourong.com/V1/Goods/topicUpAndDown",
								data: {
									token : localStorage.getItem("token"),
									goods_id: $(".liid").val(),
									status: status
								},
								dataType: "json",
								success: function(data) {
									console.log(data);
									var code = data.code;
									console.log(code);
									if(code == "1") {
										controller = 1;
										if(status == 1){
											$(".sxbtn").text("下架");
											$.toaster('上架成功');
										} else {
											$(".sxbtn").text("上架");
											$.toaster('下架成功');
										}
									} else {
										controller = 1;
										if(status == 1){
											$.toaster('上架失败');
										} else {
											$.toaster('下架失败');
										}
									}
								}
							});
						});
					}
				}
			}
		});
	});
	
	$(document).on("click","li",function(){
//		alert($(this).val()); //弹出点击的li标签的value值
//		if(controller == 1){
//			console.log($(this).val());
//		}
		localStorage.setItem("goods_id", $(this).val());
		location.href = "http://yx.fxtourong.com/V1/Goods/topicdetailser.html";
	}); 
});